﻿<?php

class Admini_AjaxController extends Uuch_BaseController{ 
    
     public function init(){
         
     }
     
     public function ttAction(){
         
         $this->content('ss');
     }

     public function uploadAction() {

        // Check the upload
        if (!isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) {
            echo "ERROR:invalid upload";
            exit(0);
        }

        // Get the image and create a thumbnail
        $img = imagecreatefromjpeg($_FILES["Filedata"]["tmp_name"]);
        if (!$img) {
            echo "ERROR:could not create image handle " . $_FILES["Filedata"]["tmp_name"];
            exit(0);
        }

        $width = imageSX($img);
        $height = imageSY($img);

        if (!$width || !$height) {
            echo "ERROR:Invalid width or height";
            exit(0);
        }

        // Build the thumbnail
        $target_width = 100;
        $target_height = 100;
        $target_ratio = $target_width / $target_height;

        $img_ratio = $width / $height;

        if ($target_ratio > $img_ratio) {
            $new_height = $target_height;
            $new_width = $img_ratio * $target_height;
        } else {
            $new_height = $target_width / $img_ratio;
            $new_width = $target_width;
        }

        if ($new_height > $target_height) {
            $new_height = $target_height;
        }
        if ($new_width > $target_width) {
            $new_height = $target_width;
        }

        $new_img = ImageCreateTrueColor($target_width, $target_height);
        if (!@imagefilledrectangle($new_img, 0, 0, $target_width - 1, $target_height - 1, 0)) {     // Fill the image black
            echo "ERROR:Could not fill new image";
            exit(0);
        }

        if (!@imagecopyresampled($new_img, $img, ($target_width - $new_width) / 2, ($target_height - $new_height) / 2, 0, 0, $new_width, $new_height, $width, $height)) {
            echo "ERROR:Could not resize image";
            exit(0);
        }

        if (!isset($_SESSION["file_info"])) {
            $_SESSION["file_info"] = array();
        }

        $path = 'data/' . date('Y-m') . '/';
        $bigPath = 'big/data/' . date('Y-m') . '/';

        $path = "../../old/public/" . $path;
        $bigPath = "../../old/public/" . $bigPath;

        Uuch_Util::create_folders($path);
        $filename = md5($_FILES["Filedata"]["tmp_name"] + rand() * 100000);

        $file_id = $path . $filename . '.jpg';
        $tt = imagejpeg($new_img, $file_id);

        Uuch_Util::create_folders($bigPath);
        move_uploaded_file($_FILES["Filedata"]["tmp_name"], $bigPath . '/' . $filename . '.jpg');

        if ($tt) {
            //echo $file_id;
            $this->json('http://cdn.uuch.com/data/'.date('Y-m').'/'.$filename.'.jpg');
//            $this->json(array("sl"=>$bigPath.$filename.'.jpg','url'=>'http://old.uuch.com/data/' . date('Y-m') . '/' . $filename . '.jpg'));
        }
        else
            $this->json(0);
    }
    public function uploadvideoAction() 
    {
        $data = $this->_getParam("data");
        if($data){
            $image = base64_decode($data);
            $filename = md5(date("ymd") . substr(time(), -5, 5) + rand() * 100000);
            $Path = '/big/data/' . date('Y-m') . '/';
            $bigPath = "../../old/public" . $Path;
            Uuch_Util::create_folders($bigPath);
            $fp = fopen($bigPath.$filename.'.jpg','w');
            fwrite($fp, $image);
            fclose($fp);
            
            $this->json(array("sl"=>$Path.$filename.'.jpg','url'=>'http://old.uuch.com/data/' . date('Y-m') . '/' . $filename . '.jpg'));
        }else{
            $this->json(0);
        }
    }
    public function deldirAction()
    {
        $dir = $this->_getParam("dir");
//        $dir = "../public" . $dir;
        $dir = "../../old/public" . $dir;
	if(!is_dir($dir)){
            @chmod("$dir", 0777);
            @unlink("$dir");
            $this->json(1);
        }else{
           $this->json(0); 
        }
    }
    public function file_group($filelj, $width, $height) {
        
        $xfilej = md5(date("ymd") . substr(time(), -5, 5) + rand() * 100000);
        $fileljp = $filelj; //图片路径，根据实际情况修改，要相对路径!!!!!!!!!!!!!
        $image = $fileljp; //图片路径 
        $img = getimagesize($image); //载入图片的函数   得到图片的信息
        switch ($img[2]) {//判断图片的类型
            case 1:
                $im = @imagecreatefromgif($image); //载入图片，创建新图片
                break;
            case 2:
                $im = @imagecreatefromjpeg($image);
                break;
            case 3:
                $im = @imagecreatefrompng($image);
                break;
        }
        $width_y = $img[0];
        $height_y = $img[1];
        if ($width_y > $height_y) {//如果宽大于高
            $width_y_y = $height_y;
            $height_y_y = $height_y;
            $jq_x = ($width_y - $height_y) / 2;
            $jq_y = 0;
        } else if ($width_y < $height_y) {//如果宽小于高
            $height_y_y = $width_y;
            $width_y_y = $width_y;
            $jq_x = 0;
            $jq_y = ($height_y - $width_y) / 2;
        } else if ($width_y == $height_y) {//如果宽小于高
            $width_y_y = $width_y;
            $height_y_y = $height_y;
            $jq_x = 0;
            $jq_y = 0;
        }
        $newim = imagecreatetruecolor($width, $height); //剪切图片第一步，建立新图像 x就是宽 ，y就是高//图片大小
        imagecopyresampled($newim, $im, 0, 0, $jq_x, $jq_y, $width, $height, $width_y_y, $height_y_y); //这个函数不失真

        imagejpeg($newim, $xfilej . ".jpg", 100); //加个100可以更清晰
        //截取图片       此处为调用页面的grouppic文件的相对路径
        return $xfilej . ".jpg"; //返回因为绝对路径
    }
    public function newuploadAction() {

        // Check the upload
        if (!isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) {
            echo "ERROR:invalid upload";
            exit(0);
        }

        // Get the image and create a thumbnail
        $img = imagecreatefromjpeg($_FILES["Filedata"]["tmp_name"]);
        if (!$img) {
            echo "ERROR:could not create image handle " . $_FILES["Filedata"]["tmp_name"];
            exit(0);
        }

        $width = imageSX($img);
        $height = imageSY($img);

        if (!$width || !$height) {
            echo "ERROR:Invalid width or height";
            exit(0);
        }

        // Build the thumbnail
        $target_width = 100;
        $target_height = 100;
        $target_ratio = $target_width / $target_height;

        $img_ratio = $width / $height;

        if ($target_ratio > $img_ratio) {
            $new_height = $target_height;
            $new_width = $img_ratio * $target_height;
        } else {
            $new_height = $target_width / $img_ratio;
            $new_width = $target_width;
        }

        if ($new_height > $target_height) {
            $new_height = $target_height;
        }
        if ($new_width > $target_width) {
            $new_height = $target_width;
        }

        $new_img = ImageCreateTrueColor($target_width, $target_height);
        if (!@imagefilledrectangle($new_img, 0, 0, $target_width - 1, $target_height - 1, 0)) {     // Fill the image black
            echo "ERROR:Could not fill new image";
            exit(0);
        }

        if (!@imagecopyresampled($new_img, $img, ($target_width - $new_width) / 2, ($target_height - $new_height) / 2, 0, 0, $new_width, $new_height, $width, $height)) {
            echo "ERROR:Could not resize image";
            exit(0);
        }

        if (!isset($_SESSION["file_info"])) {
            $_SESSION["file_info"] = array();
        }

        $path = 'data/' . date('Y-m') . '/';
        $bigPath = 'big/data/' . date('Y-m') . '/';

        $path = "../../old/public/" . $path;
        $bigPath = "../../old/public/" . $bigPath;

        Uuch_Util::create_folders($path);
        $filename = md5($_FILES["Filedata"]["tmp_name"] + rand() * 100000);

        $file_id = $path . $filename . '.jpg';
        $tt = imagejpeg($new_img, $file_id);

        Uuch_Util::create_folders($bigPath);
        move_uploaded_file($_FILES["Filedata"]["tmp_name"], $bigPath . '/' . $filename . '.jpg');

        if ($tt) {
            //echo $file_id;
            $this->json(date('Y-m') . '/' . $filename . '.jpg');
//            $this->json(array("sl"=>$bigPath.$filename.'.jpg','url'=>'http://old.uuch.com/data/' . date('Y-m') . '/' . $filename . '.jpg'));
        }
        else
            $this->json(0);
    }

    //上传图片
    public function upimgAction()
    {
        error_reporting(E_ALL);
        // $action = $_GET['act'];
        // if($action=='delimg'){
        //     $filename = $_POST['imagename'];
        //     if(!empty($filename)){
        //         unlink('files/'.$filename);
        //         echo '1';
        //     }else{
        //         echo '删除失败.';
        //     }
        // }else{
            $s       = true;
            $picname = $_FILES['mypic']['name'];
            $picsize = $_FILES['mypic']['size'];
            if ($picname != "") {
                if ($picsize > 1024000) {
                    echo '图片大小不能超过1M';
                    exit;
                }
                $type = strstr($picname, '.');
                if ($type != ".gif" && $type != ".jpg" && $type != ".png") {
                    echo '图片格式不对！';
                    exit;
                }
                $rand = rand(100, 999);
                $pics = date("YmdHis") . $rand . $type;
                //上传路径
                $pic_path = "../../old/public/expresslogo/";

                Uuch_Util::create_folders($pic_path);
                $s = move_uploaded_file($_FILES['mypic']['tmp_name'], $pic_path . $pics);
            }
            $size = round($picsize/1024,2);
            $arr = array(
                'name'  => $picname,
                'pic'   => $pics,
                'size'  => $size,
                's'     => $s,
            );
            echo json_encode($arr);
        // }
        exit;
    }
    //上传图片
    public function upimg2Action()
    {
        $picpath = $this->_getParam("picpath");
        
        $s       = true;
        $picname = $_FILES['pict_url']['name'];
        $picsize = $_FILES['pict_url']['size'];

        if ($picname != "") {
            if ($picsize > 1024000) {
                echo '图片大小不能超过1M';
                exit;
            }
            $type = strstr($picname, '.');
            if ($type != ".gif" && $type != ".jpg" && $type != ".png") {
                echo '图片格式不对！';
                exit;
            }
            $rand = rand(100, 999);
            $pics = date("YmdHis") . $rand . $type;
            //上传路径
            $pic_path = "../../old/public".$picpath;

            Uuch_Util::create_folders($pic_path);
            $s = move_uploaded_file($_FILES['pict_url']['tmp_name'], $pic_path . $pics);
        }
        $size = round($picsize/1024,2);
        $arr = array(
            'name'  => $picname,
            'pic'   => $pics,
            'size'  => $size,
            's'     => $s,
        );
        echo json_encode($arr);
        exit;
    }
}

